<?php

/**
 * Description of Order Controller
 *
 * @author Vu
 */

class Admin_Cms_OrderController extends My_Controller_Action {
	
	public function init(){
		parent::init();
		
		$auth = Zend_Auth::getInstance();
        
        if(!$auth->hasIdentity())
        { 
            $this->_redirect("user/account/login");
        }
        
        if ($this->isAccess($auth->getIdentity()->group_id, 5))
        {
			//to do
		} else {
            die("Access denied");
        }
	}   
	
	public function indexAction() {
		if($this->_request->isPost())
		{
			$checkbox = $_POST['checkbox'];
			$status = $_POST['status'];

			for($i = 0; $i < count($checkbox); $i++)
			{
				$this->editOrderStatus($checkbox[$i], $status);
			}
		}
		
        $this->view->rightAdminModules('right');
        
        $this->view->by = $this->_request->getParam("by", "a.created");
        $this->view->order = $this->_request->getParam("order", "desc");
        	
        Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
    	$model = $this->loadModel('admin_cms', 'Order');
		$this->view->paginator = $model->getAllOrders($this->_getParam('page', 1), $this->view->by.' '.$this->view->order);
		$this->view->enumStatus = Zend_Registry::get('order_status');
		
		foreach($this->view->paginator as $order)
		{
			$model = $this->loadModel("admin_cms", "Item");
	        $this->view->items = $model->getItemsByOrderId($order->order_id);
      	  
      	  	$order->deal_title = array();
      	  	$order->deal_id = array();
	        foreach($this->view->items as $item)
	        {
	            $model = $this->loadModel("admin_cms", "Deal");
	            $deal = $model->getDealById($item->deal_id);
	            $order->deal_title[] = $deal["deal_title"];
				$order->deal_id[] = $deal["deal_id"];	            
	        }
		}
    }
    
    public function editAction()
    {
        $this->view->rightAdminModules('right'); 
        
        Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
        
        $orderId = $this->_request->getParam("id");
        if($orderId == null)
        {
            $this->_redirect("admin_cms/order");
        }
        
        $this->view->orderId = $orderId;
    	$model = $this->loadModel("admin_cms", "Order");
        $this->view->order = $model->getOrderById($orderId);
        
        $model = $this->loadModel("admin_cms", "Item");
        $this->view->items = $model->getItemsByOrderId($orderId, $this->_getParam('page', 1));
        
        $this->view->deals = array();
        foreach($this->view->items as $item)
        {
            $model = $this->loadModel("admin_cms", "Deal");
            $deal = $model->getDealById($item->deal_id);
            $this->view->deals[] = $deal["deal_title"];
        }
        
        if(count($this->view->items) > 0)
        {
            $model = $this->loadModel("admin_cms", "OrderType");
            $type = $model->getOrderTypeById($item->order_type_id);
            $this->view->order_type = $type["name"];
            
            $model = $this->loadModel("admin_cms", "ShippingAddress");
            $this->view->shipping_address = $model->getShippingAddressById($item->address_id);
            
            $model = $this->loadModel("admin_user", "Account");
            $user = $model->getUserById($this->view->order->user_id);
            $this->view->email = $user->email;
        }
		
        $this->view->enumStatus = Zend_Registry::get("order_status");
        $this->view->barcode_status = Zend_Registry::get("barcode_status");
    }
    
    public function editpostAction()
    {
        if(!$this->_request->isPost())
        {
            $this->_redirect("admin_cms/order/edit");
        }
        
        // get params
        $status = $this->_request->getParam("cbxStatus");
        $orderId = $this->_request->getParam("txtId");
        
        $this->editOrderStatus($orderId, $status);
        
        $this->_redirect("admin_cms/order/edit");
    }
    
    public function deleteAction()
    {
    	// get param
    	$id = $this->_request->getParam("id");
    	if($id == null)
    	{
    		$this->_redirect("admin_cms/order");
    	}
    	
    	// delete item
    	$model = $this->loadModel("admin_cms", "Item");
    	$items = $model->deleteItemsByOrderId($id);
    	
    	// delete order
    	$model = $this->loadModel("admin_cms", "Order");
    	$order = $model->getOrderById($id);
    	$order->delete();
    	
    	$this->_redirect("admin_cms/order");
   	}
}